import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class Testconfigmap:
    url_unify = "/clusters/host"

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.configmap
    @pytest.mark.parametrize('configmapinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/configmap/1_get_configmap_list.yaml'))
    def test_get_configmap_list(self, configmapinfo):
        # 获取configmap列表接口-正常
        base_url = datas.baseUrl + Testconfigmap.url_unify
        url = base_url + configmapinfo['request']['url'].replace('{namespace}', 'default')
        method = configmapinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == configmapinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.configmap
    @pytest.mark.parametrize('configmapinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/configmap/1_get_configmap_list_exception.yaml'))
    def test_get_configmap_list_exception(self, configmapinfo):
        # 获取configmap列表接口-异常
        base_url = datas.baseUrl + Testconfigmap.url_unify
        url = base_url + configmapinfo['request']['url'].replace('{namespace}', 'default')
        method = configmapinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == configmapinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.configmap
    @pytest.mark.parametrize('configmapinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/configmap/2_create_configmap.yaml'))
    def test_create_configmap(self, configmapinfo):
        # 创建configmap接口-正常
        base_url = datas.baseUrl + Testconfigmap.url_unify
        url = base_url + configmapinfo['request']['url'].replace('{namespace}', 'default')
        method = configmapinfo['request']['method']
        data = configmapinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        configmapname = req.json()['metadata']['name']
        globals()['configmapname'] = configmapname
        assert req.status_code == configmapinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.configmap
    @pytest.mark.parametrize('configmapinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/configmap/2_create_configmap_exception.yaml'))
    def test_create_configmap_exception(self, configmapinfo):
        # 创建configmap接口-异常
        base_url = datas.baseUrl + Testconfigmap.url_unify
        url = base_url + configmapinfo['request']['url'].replace('{namespace}', 'default')
        method = configmapinfo['request']['method']
        data = configmapinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == configmapinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.configmap
    @pytest.mark.parametrize('configmapinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/configmap/3_get_specific_configmap.yaml'))
    def test_get_specific_configmap(self, configmapinfo):
        # 获取指定configmap接口-正常
        base_url = datas.baseUrl + Testconfigmap.url_unify
        url = (base_url + configmapinfo['request']['url']
               .replace('{namespace}', 'default').replace('{configmapname}', globals()['configmapname']))
        method = configmapinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == configmapinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.configmap
    @pytest.mark.parametrize('configmapinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/configmap/3_get_specific_configmap_exception.yaml'))
    def test_get_specific_configmap_exception(self, configmapinfo):
        # 获取指定configmap接口-异常
        base_url = datas.baseUrl + Testconfigmap.url_unify
        url = (base_url + configmapinfo['request']['url']
               .replace('{namespace}', 'default').replace('{configmapname}', globals()['configmapname']))
        method = configmapinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == configmapinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.configmap
    @pytest.mark.parametrize('configmapinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/configmap/4_update_configmap.yaml'))
    def test_update_configmap(self, configmapinfo):
        # 更新指定configmap接口-正常
        base_url = datas.baseUrl + Testconfigmap.url_unify
        url = (base_url + configmapinfo['request']['url']
               .replace('{namespace}', 'default').replace('{configmapname}', globals()['configmapname']))
        method = configmapinfo['request']['method']
        configmapinfo['request']['data'] = globals()['updatereqbody']
        data = configmapinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == configmapinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.configmap
    @pytest.mark.parametrize('configmapinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/configmap/4_update_configmap_exception.yaml'))
    def test_update_configmap_exception(self, configmapinfo):
        # 更新指定configmap接口-异常
        base_url = datas.baseUrl + Testconfigmap.url_unify
        url = (base_url + configmapinfo['request']['url']
               .replace('{namespace}', 'default').replace('{configmapname}', globals()['configmapname']))
        method = configmapinfo['request']['method']
        configmapinfo['request']['data'] = globals()['updatereqbody']
        data = configmapinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == configmapinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.configmap
    @pytest.mark.parametrize('configmapinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/configmap/5_update_section_configmap.yaml'))
    def test_update_section_configmap(self, configmapinfo):
        # 部分更新指定configmap的接口-正常
        base_url = datas.baseUrl + Testconfigmap.url_unify
        url = (base_url + configmapinfo['request']['url']
               .replace('{namespace}', 'default').replace('{configmapname}', globals()['configmapname']))
        method = configmapinfo['request']['method']
        data = configmapinfo['request']['data']
        header = configmapinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == configmapinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.configmap
    @pytest.mark.parametrize('configmapinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/configmap/5_update_section_configmap_exception.yaml'))
    def test_update_section_configmap_exception(self, configmapinfo):
        # 部分更新指定configmap的接口-异常
        base_url = datas.baseUrl + Testconfigmap.url_unify
        url = (base_url + configmapinfo['request']['url']
               .replace('{namespace}', 'default').replace('{configmapname}', globals()['configmapname']))
        method = configmapinfo['request']['method']
        data = configmapinfo['request']['data']
        header = configmapinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == configmapinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.configmap
    @pytest.mark.parametrize('configmapinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/configmap/6_delete_specific_configmap.yaml'))
    def test_delete_specific_configmap(self, configmapinfo):
        # 删除configmap接口-正常
        base_url = datas.baseUrl + Testconfigmap.url_unify
        url = (base_url + configmapinfo['request']['url']
               .replace('{namespace}', 'default')
               .replace('{configmapname}', globals()['configmapname']))
        method = configmapinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == configmapinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.configmap
    @pytest.mark.parametrize('configmapinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/configmap/6_delete_specific_configmap_exception.yaml'))
    def test_delete_specific_configmap_exception(self, configmapinfo):
        # 删除configmap接口-异常
        base_url = datas.baseUrl + Testconfigmap.url_unify
        url = (base_url + configmapinfo['request']['url']
               .replace('{namespace}', 'default').replace('{configmapname}', globals()['configmapname']))
        method = configmapinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == configmapinfo['validate'][0]['equals']['status_code']
